package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.Fornecedor;

public class FornecedorDaoImplementation implements FornecedorDao {

	public void save(Fornecedor fornecedor) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(fornecedor);
		transaction.commit();
		session.close();
	}

	public void update(Fornecedor fornecedor) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(fornecedor);
		transaction.commit();
		session.close();
	}

	public void delete(Fornecedor fornecedor) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(fornecedor);
		transaction.commit();
		session.close();
	}

	public void consult(Fornecedor fornecedor) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		fornecedor = (Fornecedor) session.load(Fornecedor.class, new String("Fornecedor"));
	}

	public Fornecedor getFornecedor(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Fornecedor) session.load(Fornecedor.class, id);
	}
	
	public List<Fornecedor> list() 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "SELECT new Fornecedor( f.idFornecedor, f.nome, f.nomefantasia, f.rg, f.cpf, f.telefone, f.email) FROM Fornecedor f";
        List<Fornecedor> lista = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return lista;
	}
	
	

}
